//
//  WYMomentContentCell.m
//  FriendCircleDemo
//
//  Created by WY on 2018/10/9.
//  Copyright © 2018年 WY. All rights reserved.
//

#import "WYMomentContentCell.h"
#import "WYMomentAttitudesItemViewModel.h"

@interface WYMomentContentCell ()
/// 正文
@property (nonatomic, readwrite, weak) YYLabel *contentLable;

@property (nonatomic, strong)NSIndexPath *indexPath;

@property (nonatomic, strong)UIImageView *upArrowView;
@end

@implementation WYMomentContentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+ (instancetype)cellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"MomentContentCell";
    WYMomentContentCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style indexPath:(NSIndexPath *)indexPath reuseIdentifier:(NSString *)reuseIdentifier
{
    _indexPath = indexPath;
    return [self initWithStyle:style reuseIdentifier:reuseIdentifier];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        // 设置颜色
        self.contentView.backgroundColor = color(243, 243, 245);
        // 创建自控制器
        [self setupSubViews];
    }
    return self;
}

-(void)setupSubViews
{
    /// 点击选中的颜色
    UIView *selectedView = [[UIView alloc] init];
    selectedView.backgroundColor = color(206, 210, 222);
    self.selectedBackgroundView = selectedView;
    
    /// 正文
    YYLabel *contentLable = [[YYLabel alloc] init];
    contentLable.backgroundColor = self.contentView.backgroundColor;
    /// 垂直方向顶部对齐
    contentLable.textVerticalAlignment = YYTextVerticalAlignmentTop;
    /// 异步渲染和布局
    contentLable.displaysAsynchronously = NO;
    /// 利用textLayout来设置text、font、textColor...
    contentLable.ignoreCommonProperties = YES;
    contentLable.fadeOnAsynchronouslyDisplay = NO;
    contentLable.fadeOnHighlight = NO;
    contentLable.preferredMaxLayoutWidth = WYMomentCommentViewWidth()-2*WYMomentCommentViewContentLeftOrRightInset;
    [self.contentView addSubview:contentLable];
    self.contentLable = contentLable;
    
    /// 分割线
    
    UIImageView *divider = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wx_albumCommentHorizontalLine_33x1"]];
    divider.backgroundColor = LineColor;
    self.divider = divider;
    [self.contentView addSubview:divider];
    
    /// 向上的箭头
    
    UIImageView *upArrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wx_albumTriangleB_45x6"]];
    self.upArrowView = upArrowView;
    [self.contentView addSubview:upArrowView];
}

-(void)setContentItemModel:(WYMomentContentItemViewModel *)contentItemModel
{
    _contentItemModel = contentItemModel;
    _contentLable.textLayout = contentItemModel.contentLableLayout;
    _contentLable.frame = contentItemModel.contentLableFrame;
    if ([contentItemModel isKindOfClass:[WYMomentAttitudesItemViewModel class]]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }else{
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
    }
    if (_indexPath.row == 0) {
        _upArrowView.hidden = NO;
    }else{
        _upArrowView.hidden = YES;
    }
//    [self.superview bringSubviewToFront:self];
}

#pragma mark - Override
/// PS:重写cell的尺寸 这是评论View关键
- (void)setFrame:(CGRect)frame{
    frame.origin.x = WYMomentContentLeftOrRightInset+WYMomentAvatarWH+WYMomentContentInnerMargin;
    frame.size.width = WYMomentCommentViewWidth();
    [super setFrame:frame];
}

#pragma mark - 布局
- (void)layoutSubviews{
    [super layoutSubviews];
    self.divider.frame =CGRectMake(0, self.height-0.5, self.width, 0.5);

    self.upArrowView.size = CGSizeMake(WYMomentUpArrowViewWidth, WYMomentUpArrowViewHeight);
    self.upArrowView.bottom = 0;
    self.upArrowView.left = 5.0;
}

@end
